///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_APP_CFINISHFUNCTION_H__
#define __BUOLA_APP_CFINISHFUNCTION_H__

#include <buola/buola.h>

namespace buola {

///\addtogroup application
///@{

///////////////////////////////////////////////////////////////////////////

/// function to be called when the program finishes

/// When an object of this class is created, it registers a function to
/// be called during application cleanup.
///
/// Objects of this class should never be destroyed, except after the
/// application terminates. They should either be global or
/// static member variables.
///
///////////////////////////////////////////////////////////////////////////

class CFinishFunction : public UUncopiable
{
private:
	//! signature for finalization functions
	typedef bool(*tFunction)();

public:
	//! registers the function
	CFinishFunction(tFunction pFunc,const std::wstring &pName,int pPriority);

    //! run all finalization functions
    static bool RunAll();

private:
	//! runs the function itself
	bool Run();

	//! internal management
	static CFinishFunction *GetSet(CFinishFunction* =nullptr);

	//! compare function to sort by priority
	static bool GenericCompare(CFinishFunction *p1,CFinishFunction *p2);

private:
	tFunction mFunc;
	std::wstring mName;
	bool mDone;
	int mPriority;

	friend class CApp;
};

///@}

/*namespace buola*/ }

#endif
